package com.lbn.monitor.app.account;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.free.framework.lib.utils.PrefsMgr;
import com.lbn.monitor.App;
import com.lbn.monitor.R;
import com.lbn.monitor.app.account.service.DownloadService;
import com.lbn.monitor.app.login.LoginActivity;
import com.lbn.monitor.base.AppBaseFragment;
import com.lbn.monitor.util.AppUtils;
import com.lbn.monitor.util.Constant;
import com.lbn.monitor.util.UPushSdk;
import com.suke.widget.SwitchButton;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AccountFragment extends AppBaseFragment<AccountPresenter> implements IAccountView, SwitchButton.OnCheckedChangeListener {


    private static final String TAG = "AccountFragment";

    @BindView(R.id.switch_warn)
    SwitchButton switchWarn;
    @BindView(R.id.switch_voice)
    SwitchButton switchVoice;
    @BindView(R.id.switch_shake)
    SwitchButton switchShake;
    @BindView(R.id.version_name)
    TextView versionName;

    private boolean warn;
    private boolean voice;
    private boolean shake;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_account, null, false);
        ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public AccountPresenter createPresenter() {
        return new AccountPresenter();
    }

    @Override
    protected void onPresenterCreated() {
        super.onPresenterCreated();

        warn = (boolean) PrefsMgr.get(getContext(), Constant.SWITCH_WARN, true);
        voice = (boolean) PrefsMgr.get(getContext(), Constant.SWITCH_VOICE, true);
        shake = (boolean) PrefsMgr.get(getContext(), Constant.SWITCH_SHAKE, true);

        switchWarn.setChecked(warn);
        switchVoice.setChecked(voice);
        switchShake.setChecked(shake);

        switchWarn.setOnCheckedChangeListener(this);
        switchVoice.setOnCheckedChangeListener(this);
        switchShake.setOnCheckedChangeListener(this);
        versionName.setText("当前版本v"+AppUtils.getVerionName(getContext()));
    }

    public static AccountFragment newInstance() {
        Bundle args = new Bundle();
        AccountFragment fragment = new AccountFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @OnClick({R.id.cpw, R.id.exit, R.id.check_update,R.id.about_us})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cpw:
                CpwActivity.startActivity(getContext());
                break;
            case R.id.exit:
                App.clearUser();
                LoginActivity.startActivity(getContext());
                getActivity().finish();
                break;
            case R.id.check_update:
                int code = AppUtils.getVersionCode(getContext());
                mPresenter.checkUpdate(code);
                break;
            case R.id.about_us:
                AboutUsActivity.startActivity(getContext());
                break;
        }
    }


    @Override
    public void onCheckedChanged(SwitchButton view, boolean isChecked) {

        switch (view.getId()) {
            case R.id.switch_warn:
                if (isChecked) {
                    PrefsMgr.putAndApply(getContext(), Constant.SWITCH_WARN, true);
                    UPushSdk.instance().enable(getContext());
                } else {
                    PrefsMgr.putAndApply(getContext(), Constant.SWITCH_WARN, false);
                    switchVoice.setChecked(false);
                    switchShake.setChecked(false);
                    UPushSdk.instance().disable(getContext());
                }
                break;
            case R.id.switch_voice:
                if (isChecked) {
                    PrefsMgr.putAndApply(getContext(), Constant.SWITCH_VOICE, true);
                    if (!switchWarn.isChecked()) {
                        switchWarn.setChecked(true);
                    }
                } else {
                    PrefsMgr.putAndApply(getContext(), Constant.SWITCH_VOICE, false);
                }

                UPushSdk.instance().voiceSwitch(getContext(), isChecked);

                break;
            case R.id.switch_shake:
                if (isChecked) {
                    PrefsMgr.putAndApply(getContext(), Constant.SWITCH_SHAKE, true);
                    if (!switchWarn.isChecked()) {
                        switchWarn.setChecked(true);
                    }
                } else {
                    PrefsMgr.putAndApply(getContext(), Constant.SWITCH_SHAKE, false);
                }

                UPushSdk.instance().shakeSwitch(getContext(), isChecked);
                break;
        }

        //丢弃逻辑QUQ
//        switch (view.getId()){
//            case R.id.switch_warn:
//                if(isChecked){
//                    PrefsMgr.putAndApply(getContext(),Constant.SWITCH_WARN,true);
//                    if(!switchVoice.isChecked()){
//                        switchVoice.setChecked(true);
//                    }
//                    if(!switchShake.isChecked()){
//                        switchShake.setChecked(true);
//                    }
//                }else{
//                    PrefsMgr.putAndApply(getContext(),Constant.SWITCH_WARN,false);
//                    if(switchVoice.isChecked()){
//                        switchVoice.setChecked(false);
//                    }
//                    if(switchShake.isChecked()){
//                        switchShake.setChecked(false);
//                    }
//                }
//                break;
//            case R.id.switch_voice:
//                if(isChecked){
//                    PrefsMgr.putAndApply(getContext(),Constant.SWITCH_VOICE,true);
//                    if(!switchWarn.isChecked()){
//                        //如果消息提醒没有打开的话，打开。
//                        switchWarn.setChecked(true);
////                        PrefsMgr.putAndApply(getContext(),Constant.SWITCH_WARN,true);
//                    }
//                }else{
//                    //如果震动也关闭了，把消息提醒也关闭。
//                    PrefsMgr.putAndApply(getContext(),Constant.SWITCH_VOICE,false);
//                    if(!switchShake.isChecked()&&switchWarn.isChecked()){
//                        switchWarn.setChecked(false);
//                    }
//                }
//                break;
//            case R.id.switch_shake:
//                if(isChecked){
//                    PrefsMgr.putAndApply(getContext(),Constant.SWITCH_SHAKE,true);
//                    if(!switchWarn.isChecked()){
//                        //如果消息提醒没有打开的话，打开。
//                        switchWarn.setChecked(true);
////                        PrefsMgr.putAndApply(getContext(),Constant.SWITCH_WARN,true);
//                    }
//                }else{
//                    //如果声音也关闭了，把消息提醒也关闭。
//                    PrefsMgr.putAndApply(getContext(),Constant.SWITCH_SHAKE,false);
//                    if(!switchVoice.isChecked()&&switchWarn.isChecked()){
//                        switchWarn.setChecked(false);
////                        PrefsMgr.putAndApply(getContext(),Constant.SWITCH_SHAKE,false);
//                    }
//                }
//                break;
//        }


    }

    @Override
    public void newVersion(String url) {
        Intent intent = new Intent(getContext(),DownloadService.class);
        intent.putExtra(Constant.UPDATE_VERSION,url);
        getContext().startService(intent);
    }
}
